<template>
	<div class="content">
		<h2><i class="iconfont icon-jinbi"></i>现金货款管理</h2>
		<div class="content-wrap">
			<!--搜索分类-->
			<div class="search">
				<div class="search__list">
					<div class="search__list--title search__list--title-tip">状态</div>
					<el-select v-model="params.is_agree" size="mini" @change="selctCheckState">
						<el-option label="全部" :value="-1"></el-option>
						<el-option label="未审核" :value="''"></el-option>
						<el-option label="已驳回" :value="0"></el-option>
						<el-option label="已通过" :value="1"></el-option>
					</el-select>
				</div>
				<div class="search__list">
					<div class="search__list--title">申请结算时间</div>
					<el-date-picker
						size="mini"
						@change="handleSelectTimes"
					    v-model="times"
					    type="daterange"
					    range-separator="至"
					    start-placeholder="开始日期"
					    end-placeholder="结束日期">
				  	</el-date-picker>
				</div>
				<div class="search__list">
					<div class="search__list--title">支付时间</div>
					<el-date-picker
						size="mini"
						@change="handleSelectTimes2"
					    v-model="times2"
					    type="daterange"
					    range-separator="至"
					    start-placeholder="开始日期"
					    end-placeholder="结束日期">
				  	</el-date-picker>
				</div>
				<div class="search__list" align="center" style="display: none;">
					<div class="search__list--title">所属区域</div>
					<el-cascader
						size="mini"
						clearable
					  :options="[{label:'aaa', value: 111}]"
					></el-cascader>
				</div>
				
				<div class="search__list">
					<div class="search__list--title">会员号</div>
					<el-input placeholder="供应商账号" v-model="params.mobile" clearable size="mini"></el-input>
				</div>
				<div class="search__list">
					<div class="search__list--title">结算订单号</div>
					<el-input placeholder="结算订单号" v-model="params.company_cash_id" clearable size="mini"></el-input>
				</div>
				<div class="search__list">
					<el-button type="primary" @click="fetchDianyuan" size="mini">查找</el-button>
				</div>
			</div>
			<!--商品展示表格-->
			<div class="content-table" v-loading="loading" >
				<el-table :data="data" strip size="mini">
					<el-table-column label="订单号" prop="company_cash_id"></el-table-column>
					<el-table-column label="会员名称" prop="real_name"></el-table-column>
					<el-table-column label="会员号" prop="mobile" width="120"></el-table-column>
					<el-table-column label="商户名称" prop="company.company_name"></el-table-column>
					<el-table-column label="银行账户名" prop="bankCard.bank_card_name"></el-table-column>
					<el-table-column label="账户类型" prop="bankCard.bank_card_type"></el-table-column>
					<el-table-column label="银行账户" prop="bankCard.card_no"></el-table-column>
					<el-table-column label="开户行" prop="bankCard.bank_name"></el-table-column>
					<el-table-column label="结算金融" prop="cash_money">
						<template slot-scope="scope" >
							<span>{{scope.row.cash_money}}元</span>
						</template>
					</el-table-column>
					<el-table-column label="申请时间" prop="cash_time">
						<template slot-scope="scope" >
							<span v-if="scope.row.cash_time">{{scope.row.cash_time|formatTime}}</span>
						</template>
					</el-table-column>
					<el-table-column label="结算时间" prop="check_time">
						<template slot-scope="scope" >
							<span v-if="scope.row.check_time">{{scope.row.check_time|formatTime}}</span>
						</template>
					</el-table-column>
					<el-table-column label="状态">
						<template slot-scope="scope" >
							<el-tag v-if="null == scope.row.is_agree" type="warning">未审核</el-tag>
							<el-tag v-if="scope.row.is_agree == 0" type="danger">已拒绝</el-tag>
							<el-tag v-if="scope.row.is_agree == 1" type="success">已通过</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="支付结果">
						<template slot-scope="scope" >
							<span v-if="scope.row.platPayRcd">支付{{scope.row.platPayRcd.pay_state}}</span>
						</template>
					</el-table-column>
					<el-table-column label="操作" align="center" width="300">
						<template slot-scope="scope" >
							<el-button 
								v-if="null == scope.row.is_agree"
								size="mini"
								type="success"
								@click="agreeDianyuan(scope.row.company_cash_id)">
								同意
							</el-button>
							<el-button
								v-if="null == scope.row.is_agree"
								size="mini"
								type="danger"
								@click="rejectDianyuan(scope.row.company_cash_id)">
								拒绝
							</el-button>
							<el-button 
								v-if="scope.row.is_agree == 1 && scope.row.bankCard && scope.row.bankCard.bank_card_type == '企业' && !scope.row.platPayRcd"
								size="mini"
								type="success"
								@click="payOk(scope.row.company_cash_id)">
								确认支付成功
							</el-button>
							<el-button 
								v-if="scope.row.bankCard && scope.row.bankCard.bank_card_type == '个人' && scope.row.platPayRcd && scope.row.platPayRcd.pay_state == '处理中'"
								size="mini"
								type="success"
								@click="findPay(scope.row.platPayRcd.businessnumber)">
								查询支付结果
							</el-button>
							<el-button 
								v-if="scope.row.bankCard && scope.row.bankCard.bank_card_type == '个人' && scope.row.platPayRcd && scope.row.platPayRcd.pay_state == '失败'"
								size="mini"
								type="success"
								@click="startPay(scope.row.platPayRcd.businessnumber)">
								重新发起支付
							</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<el-pagination 
				@current-change="handleCurrentChange"
				background 
				layout="total, prev, pager, next, jumper"
				:total="totalNum" 
				class="fr"
				>
			</el-pagination>
		</div>
	</div>
</template>

<script>

	export default {
		created() {
			this.fetchDianyuan();
		},
		components: {
			
		},
		data() {
			return {
				params: {
					cash_type: 1,
					is_agree: -1,
					start_time: "",
					end_time: "",
					start_time2: "",
					end_time2: "",
					company_cash_id: null,
					mobile: null,
					page: 1,
					rows: 10
				},
				showModel: false,			//显示申请详情
				data: null,					//提现数据			
				times: "",					//申请日期
				times2: "",					//结算日期
				loading: true,				//加载小圈圈
				totalNum: 0					//返回数据总数，用于分页那里显示
			}
		},
		methods: {
			//翻页
			handleCurrentChange(pages) {
				this.params.page = pages;
				this.fetchDianyuan();
			},
			//选择时间时候触发
			handleSelectTimes(val) {
				if (this.times) {
					this.params.start_time = this.times[0].getTime();
					this.params.end_time = this.times[1].getTime();
				}else {
					this.params.start_time = "";
					this.params.end_time = "";
				}
			},
			//选择时间时候触发
			handleSelectTimes2(val) {
				if (this.times2) {
					this.params.start_time2 = this.times2[0].getTime();
					this.params.end_time2 = this.times2[1].getTime();
				}else {
					this.params.start_time2 = "";
					this.params.end_time2 = "";
				}
			},
			//选择审核状态0代表未审核，1代表已审核
			selctCheckState(val) {
				this.params.is_agree   = val;
			},
			fetchDianyuan() {
				this.loading = true;
				var url = `${this.url}/company/cash_get_check_list.action`;
				var params = this.params;
				this.$http.get(url,{ params }).then((res)=> {
					this.totalNum = res.data.result.total;
					this.loading = false;
					this.data = res.data.result.rows;
				});
			},
			agreeDianyuan(id) {
				this.$confirm("确定同意申请？该操作不能退回！", "同意申请", {
					cancelButtonText: '取消',
					confirmButtonText: "确定",
				}).then(({ value })=> {
					this.loading = true;
					var url = `${this.url}/company/cash_check.action`;
					var params = {
						company_cash_id: id,
						is_agree: 1,
						reject_reason: "",
					};
					this.$http.get(url,{ params }).then((res)=> {
						if( res.data.success ) {
							this.fetchDianyuan();
						}else {
							this.$message({
								type: "error",
								message: res.data.result.errmsg
							});
						}
					});
				});
			},
			rejectDianyuan(id) {
				this.$prompt("请填写拒绝理由", "拒绝申请", {
					cancelButtonText: '取消',
					confirmButtonText: "确定",
				}).then(({ value })=> {
					if (value === null || value.replace(/\s/g, "") === "") {
						this.$message({
							type: "error",
							message: "拒绝失败，理由不能为空"
						});
						return;
					}
					var url = `${this.url}/company/cash_check.action`;
					var params = {
						company_cash_id: id,
						is_agree: 0,
						reject_reason: value,
					};
					this.$http.get(url,{ params }).then((res)=> {
						this.loading = true;
						if( res.data.success ) {
							this.fetchDianyuan();
						}else {
							this.$message({
								type: "error",
								message: res.data.result.errmsg
							});
						}
					});
				}).catch(()=>{
					
				});
			},
			payOk(company_cash_id) {
				this.$confirm("确定已经转账到商家账户了吗？该操作不能退回！", "确认支付成功", {
					cancelButtonText: '取消',
					confirmButtonText: "确定",
				}).then(({ value })=> {
					this.loading = true;
					var url = `${this.url}/company/cash_pay_ok.action`;
					var params = {
						company_cash_id: company_cash_id
					};
					this.$http.get(url,{ params }).then((res)=> {
						this.loading = false;
						if( res.data.success ) {
							this.$message({
								type: "success",
								message: '操作成功'
							});
						}else {
							this.$message({
								type: "error",
								message: res.data.result.errmsg
							});
						}
						this.fetchDianyuan();
					});
				});
			},
			findPay(businessnumber) {
				this.loading = true;
				var url = `${this.url}/pay/chuanhua_find_pay.action`;
				var params = {
					businessnumber: businessnumber
				};
				this.$http.get(url,{ params }).then((res)=> {
					this.loading = false;
					if( res.data.success ) {
						var msg = '';
						if(res.data.result) {
							msg = '支付' + res.data.result;
						} else {
							msg = '查询失败！请重试！';
						}
						this.$message({
							type: "success",
							message: msg 
						});
						this.fetchDianyuan();
					}else {
						this.$message({
							type: "error",
							message: res.data.result.errmsg
						});
					}
				});
			},
			startPay(businessnumber) {
				this.$confirm("确认重新发起支付？", "重新发起支付", {
					cancelButtonText: '取消',
					confirmButtonText: "确定",
				}).then(({ value })=> {
					this.loading = true;
					var url = `${this.url}/pay/chuanhua_start_pay.action`;
					var params = {
						businessnumber: businessnumber
					};
					this.$http.get(url,{ params }).then((res)=> {
						this.loading = false;
						if( res.data.success ) {
							this.$message({
								type: "success",
								message: "发起支付成功" 
							});
							this.fetchDianyuan();
						}else {
							this.$message({
								type: "error",
								message: res.data.result.errmsg
							});
						}
					});
				});
			}
		}
	}
</script>

<style scoped lang="scss">

</style>